#coding=utf-8
#Boa:Dialog:DlgAccountEditor

import wx
from product import CDataAccount

def create(parent):
    return DlgAccountEditor(parent)

[wxID_DLGACCOUNTEDITOR, wxID_DLGACCOUNTEDITORBTNCANCEL, 
 wxID_DLGACCOUNTEDITORBTNSAVE, wxID_DLGACCOUNTEDITORCHECKBOX_DISABLED, 
 wxID_DLGACCOUNTEDITORTBRIEFNAME, wxID_DLGACCOUNTEDITORTCBRIEFNAME, 
 wxID_DLGACCOUNTEDITORTCMEMO, wxID_DLGACCOUNTEDITORTCNAME, 
 wxID_DLGACCOUNTEDITORTCOPENBALANCE, wxID_DLGACCOUNTEDITORTMEMO, 
 wxID_DLGACCOUNTEDITORTNAME, wxID_DLGACCOUNTEDITORTOPENBALANCE, 
] = [wx.NewId() for _init_ctrls in range(12)]

class DlgAccountEditor(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGACCOUNTEDITOR,
              name=u'DlgAccountEditor', parent=prnt, pos=wx.Point(645, 314),
              size=wx.Size(542, 389), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u8d26\u6237\u7f16\u8f91')
        self.SetClientSize(wx.Size(526, 351))

        self.tBriefName = wx.StaticText(id=wxID_DLGACCOUNTEDITORTBRIEFNAME,
              label=u'\u7b80\u540d\uff1a', name=u'tBriefName', parent=self,
              pos=wx.Point(48, 32), size=wx.Size(36, 14), style=0)

        self.tcBriefName = wx.TextCtrl(id=wxID_DLGACCOUNTEDITORTCBRIEFNAME,
              name=u'tcBriefName', parent=self, pos=wx.Point(112, 24),
              size=wx.Size(176, 22), style=0, value=u'')

        self.tName = wx.StaticText(id=wxID_DLGACCOUNTEDITORTNAME,
              label=u'\u5168\u540d\uff1a', name=u'tName', parent=self,
              pos=wx.Point(48, 64), size=wx.Size(36, 14), style=0)

        self.tcName = wx.TextCtrl(id=wxID_DLGACCOUNTEDITORTCNAME,
              name=u'tcName', parent=self, pos=wx.Point(112, 64),
              size=wx.Size(312, 22), style=0, value=u'')

        self.tMemo = wx.StaticText(id=wxID_DLGACCOUNTEDITORTMEMO,
              label=u'\u5907\u6ce8\uff1a', name=u'tMemo', parent=self,
              pos=wx.Point(48, 152), size=wx.Size(36, 14), style=0)

        self.tcMemo = wx.TextCtrl(id=wxID_DLGACCOUNTEDITORTCMEMO,
              name=u'tcMemo', parent=self, pos=wx.Point(112, 152),
              size=wx.Size(312, 64), style=wx.TE_MULTILINE, value=u'')

        self.tOpenBalance = wx.StaticText(id=wxID_DLGACCOUNTEDITORTOPENBALANCE,
              label=u'\u671f\u521d\u4f59\u989d\uff1a', name=u'tOpenBalance',
              parent=self, pos=wx.Point(48, 104), size=wx.Size(60, 14),
              style=0)

        self.tcOpenBalance = wx.TextCtrl(id=wxID_DLGACCOUNTEDITORTCOPENBALANCE,
              name=u'tcOpenBalance', parent=self, pos=wx.Point(112, 104),
              size=wx.Size(100, 22), style=0, value=u'')
        self.tcOpenBalance.SetEditable(False)
        self.tcOpenBalance.Enable(False)

        self.btnSave = wx.Button(id=wxID_DLGACCOUNTEDITORBTNSAVE,
              label=u'\u4fdd\u5b58', name=u'btnSave', parent=self,
              pos=wx.Point(120, 288), size=wx.Size(75, 24), style=0)
        self.btnSave.Bind(wx.EVT_BUTTON, self.OnBtnSaveButton,
              id=wxID_DLGACCOUNTEDITORBTNSAVE)

        self.btnCancel = wx.Button(id=wxID_DLGACCOUNTEDITORBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(288, 288), size=wx.Size(75, 24), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGACCOUNTEDITORBTNCANCEL)

        self.checkBox_disabled = wx.CheckBox(id=wxID_DLGACCOUNTEDITORCHECKBOX_DISABLED,
              label=u'\u505c\u7528', name=u'checkBox_disabled', parent=self,
              pos=wx.Point(48, 232), size=wx.Size(79, 14), style=0)
        self.checkBox_disabled.SetValue(True)

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.myInit()
        
        
    def myInit(self):
        self.curObj = None
        
    def getObj(self):
        return self.curObj
        
    def setData(self, obj):
        self.curObj = obj

        cls = obj.__class__
        self.tcBriefName.SetValue(obj.getFieldVal(index=cls.COL_BRIEFNAME))
        self.tcName.SetValue(obj.getFieldVal(index=cls.COL_FULLNAME))
        self.tcMemo.SetValue(obj.getFieldVal(index=cls.COL_MEMO))
        self.checkBox_disabled.SetValue(obj.getFieldVal(index=cls.COL_DISABLED))
        
    def OnBtnSaveButton(self, event):
        if (self.tcBriefName.GetValue() == ''):
            dlg = wx.MessageDialog(self, u'必须设置账户简名用于收支款项管理', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        data = {CDataAccount.COL_BRIEFNAME:self.tcBriefName.GetValue(),
                CDataAccount.COL_FULLNAME:self.tcName.GetValue(),
                CDataAccount.COL_MEMO:self.tcMemo.GetValue(),
                CDataAccount.COL_DISABLED:int(self.checkBox_disabled.GetValue())}
        
        if (self.curObj is not None):
            self.curObj.setMulFieldVal(data)
            self.curObj.update()
        else:
            obj = CDataAccount.factory()
            #初始化时已经默认获取了新id
            #data[CDataAccount.COL_ID] = CDataStaff.newAutoIncrId()
            # 140711
            #obj.assignNewId()
            obj.setMulFieldVal(data)
            obj.insert()
            self.curObj = obj

        #self.Close()
        self.EndModal(wx.ID_OK)
        
    def OnBtnCancelButton(self, event):
        #self.Close()
        self.EndModal(wx.ID_CANCEL)
